<template>
  <div class="index">
    <el-container style="height: 100vh; border: 1px solid #eee">
      <el-aside style="width: auto;background:#001529;">
        <el-menu
          class="el-menu-vertical"
          background-color="#001529"
          text-color="#a0a1a3"
          router
          active-text-color="#fff"
          :default-active="defaultActive"
          :collapse="isCollapse"
        >
          <h3 class="logo">
            <span v-if="!isCollapse">后台管理</span>
            <img style="height:40px;width:40px;border-radius:50%;" v-else src="../assets/logo.jpg" alt="">
          </h3>
          <el-submenu popper-class="test" :index="''+index" v-for="(item, index) in menus" :key="index">
            <template slot="title" v-if="item.path==='users'">
              <i class="el-icon-user-solid"></i>
              <span slot="title">{{item.authName}}</span>
            </template>
            <template slot="title" v-if="item.path==='rights'">
              <i class="el-icon-more"></i>
              <span slot="title">{{item.authName}}</span>
            </template>
            <template slot="title" v-if="item.path==='goods'">
              <i class="el-icon-s-goods"></i>
              <span slot="title">{{item.authName}}</span>
            </template>
            <template slot="title" v-if="item.path==='orders'">
              <i class="el-icon-s-order"></i>
              <span slot="title">{{item.authName}}</span>
            </template>
            <template slot="title" v-if="item.path==='reports'">
              <i class="el-icon-s-data"></i>
              <span slot="title">{{item.authName}}</span>
            </template>
            <el-menu-item
              v-for="(_item, _index) in item.children"
              :key="_index"
              :index="'/index/'+_item.path"
            >{{_item.authName}}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-container>
        <el-header class="self-header">
          <i v-if="!isCollapse" class="el-icon-s-fold" @click="handleCollapse"></i>
          <i v-else class="el-icon-s-unfold" @click="handleCollapse"></i>
          <el-dropdown class="self-dropdown">
            <i class="el-icon-setting" style="margin-right: 15px"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="logOut">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <span>{{$store.state.username}}</span>
        </el-header>

        <el-main style="background:#eee;">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { getMenus } from "@/http/apis";
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      menus: [],
      defaultActive: "",
      isCollapse: true
    };
  },
  watch: {
    $route: {
      handler(val) {
        console.log(val);
        this.defaultActive = val.path;
      },
      immediate: true
    }
  },
  methods: {
    ...mapMutations(['initState']),
    logOut() {
      console.log(123);
      this.initState();
      this.$router.push({
        name: "login"
      });
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    }
  },
  async created() {
    const res = await getMenus();
    this.menus = res;
  }
};
</script>
<style >
.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.index .el-header {
  background-color: #ffffff;
  color: #333;
  line-height: 60px;
  border-bottom: 1px solid #eee;
}

.index .logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
}
.index .el-menu {
  border-right: 0;
}
.index .self-header {
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.index .self-dropdown {
  flex: 1;
  display: flex;
  justify-content: flex-end;
}
.index .el-icon-s-fold,
.index .el-icon-s-unfold {
  font-size: 20px;
  -webkit-font-smoothing: antialiased;
}
</style>